﻿namespace Leo.Common.RabbitMQ
{

    /// <summary>
    ///  RabbitMQ  配置类
    /// </summary>
    public class RabbitConfig
    {
        internal ushort Qos { get; set; }

        /// <summary>
        /// IP 地址
        /// </summary>
        internal string Host { get; set; }

        /// <summary>
        /// 端口 默认 5672
        /// </summary>
        internal int Port { get; set; } = 5672;

        /// <summary>
        /// 账号
        /// </summary>
        internal string UserName { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        internal string Password { get; set; }


        /// <summary>
        /// 虚拟主机
        /// </summary>
        public string VirtualHost { get; set; }


        /// <summary>
        /// 连接字符串
        /// </summary>
        public string ConnectionString { get; set; }

        /// <summary>
        /// 链接名称
        /// </summary>
        public string ConnectionName { get; set; }


        /// <summary>
        /// 是否自动删除队列
        /// </summary>
        public bool AutoDeleteQueue { get; set; }

        /// <summary>
        /// 是否启用 队列查询
        /// </summary>
        public bool EnableQueueAutoDiscovery { get; set; }


        /// <summary>
        /// 消费者分配独立 通道
        /// </summary>
        public bool AssignIndependentChannelToEachConsumer { get; set; }



        public RabbitConfig(string connectionString)
        {
            this.ConnectionString = connectionString;
            this.AnalysisConnectionString(connectionString);

            if (this.Host is null)
            {
                throw new ArgumentException("RabbitMQ连接字符串有错误，缺少Rabbit服务地址");
            }
            if (this.UserName is null)
            {
                throw new ArgumentException("RabbitMQ连接字符串有错误，缺少用户名");
            }
            if (this.Password is null)
            {
                throw new ArgumentException("RabbitMQ连接字符串有错误，缺少用户密码");
            }

        }

        /// <summary>
        /// 解析连接字符串
        /// </summary>
        /// <param name="connectionString">字符串</param>
        private void AnalysisConnectionString(string connectionString)
        {
            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new ArgumentNullException(nameof(connectionString), "RabbitMQ连接字符串有误！");
            }


            //host=127.0.0.1;port=5672;user=admin;pwd=123456;vhost=myhost;qos=1
            foreach (var text in connectionString.Split(';', StringSplitOptions.RemoveEmptyEntries))
            {
                var parts = text.Split('=', StringSplitOptions.None);
                if (parts == null || parts.Length != 2)
                {
                    throw new ArgumentException($"无效的RabbitMQ连接字符串[{text}]");
                }

                var key = parts[0].ToUpper();
                var value = parts[1];

                switch (key)
                {
                    case "HOST":
                    case "SERVER":
                        this.Host = value;
                        break;
                    case "PORT":
                        this.Port = Convert.ToInt32(value);
                        break;
                    case "USER":
                    case "UID":
                    case "USERID":
                    case "USERNAME":
                        this.UserName = value;
                        break;
                    case "PWD":
                    case "PASSWD":
                    case "PASSWORD":
                        this.Password = value;
                        break;
                    case "VHOST":
                    case "VIRTUALHOST":
                        this.VirtualHost = value;
                        break;
                    case "QOS":
                        this.Qos = Convert.ToUInt16(value);
                        break;
                    default:
                        throw new ArgumentException($"无效的RabbitMQ连接字符串[{text}]");
                }
            }


        }



    }
}
